﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using NPOI.XWPF.UserModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;

namespace JX.Infrastructure.Common
{
	/// <summary>
	/// 对Microsoft.AspNetCore.Http.HttpContext对象的扩展。
	/// 使用方法：
	/// 1、在Startup.cs文件的ConfigureServices方法中，添加services.AddMyHttpContextAccessor();
	/// 2、修改Configure方法，添加代码：app.UseStaticMyHttpContext();
	/// </summary>
	public static class MyHttpContext
	{
		private static IHttpContextAccessor _contextAccessor;

		/// <summary>
		/// 得到当前HTTP 请求的HttpContext 对象
		/// </summary>
		public static HttpContext Current => _contextAccessor.HttpContext;

		/// <summary>
		/// 当前登录用户的ID
		/// </summary>
		public static string CurrentUserID
		{
			get
			{
				string result = string.Empty;
				var claim = Current.User.FindFirst(ClaimTypes.Sid);
				if (claim != null)
				{
					result = claim.Value;
				}
				return result;
			}
		}
		/// <summary>
		/// 当前登录用户的名称
		/// </summary>
		public static string CurrentUserName
		{
			get
			{
				string result = string.Empty;
				var claim = Current.User.FindFirst(ClaimTypes.Name);
				if(claim != null)
				{
					result = claim.Value;
				}
				return result;
			}
		}
		/// <summary>
		/// 当前登录用户的角色(会员组)ID，多个ID用“,”分隔
		/// </summary>
		public static string CurrentRoleID
		{
			get
			{
				string result = string.Empty;
				var claim = Current.User.FindFirst("RoleID");
				if (claim != null)
				{
					result = claim.Value;
				}
				return result;
			}
		}
		/// <summary>
		/// 当前登录用户的权限列表
		/// </summary>
		public static List<Claim> CurrentRoles
		{
			get
			{
				var list = Current.User.FindAll(ClaimTypes.Role).ToList();
				if (list == null)
				{
					list = new List<Claim>();
				}
				return list;
			}
		}
		/// <summary>
		/// 当前登录用户是否超级管理员
		/// </summary>
		public static bool IsSuperAdmin
		{
			get
			{
				bool result = false;
				foreach (var item in CurrentRoles)
				{
					if (item.Value == "SuperAdmin")
					{
						result = true;
						break;
					}
				}
				return result;
			}
		}
		/// <summary>
		/// 判断传入的角色ID，是否在当前管理员所属的角色列表中
		/// </summary>
		/// <param name="roleid"></param>
		/// <returns></returns>
		public static bool IsInRoleID(string roleid)
		{
			if (string.IsNullOrEmpty(CurrentRoleID))
				return false;
			return StringHelper.FoundCharInArr(roleid, CurrentRoleID);
		}

		/// <summary>
		/// 注入IHttpContextAccessor对象到内部属性中
		/// </summary>
		/// <param name="contextAccessor"></param>
		public static void Configure(IHttpContextAccessor contextAccessor)
		{
			_contextAccessor = contextAccessor;
		}

		/// <summary>
		/// 在Startup.cs文件的ConfigureServices方法中注册服务
		/// </summary>
		/// <param name="services"></param>
		public static void AddMyHttpContextAccessor(this IServiceCollection services)
		{
			services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
		}
		/// <summary>
		/// 在Startup.cs文件的Configure方法中启用服务
		/// </summary>
		/// <param name="app"></param>
		/// <returns></returns>
		public static IApplicationBuilder UseStaticMyHttpContext(this IApplicationBuilder app)
		{
			var httpContextAccessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
			Configure(httpContextAccessor);
			return app;
		}
	}
}
